package com.ejiaoyi.api.config;

import com.ejiaoyi.common.GlobalConfig;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * 数据源及事务配置
 *
 * @author unknownChivalrous
 * @since 2020-03-17
 */
@Aspect
@Configuration
public class DataSourceConfig {
    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.ejiaoyi.api.service.*.*(..))";

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean("transactionInterceptor1")
    public TransactionInterceptor transactionInterceptor() {
        return GlobalConfig.txAdviceBean(platformTransactionManager);
    }

    @Bean
    public Advisor advisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, transactionInterceptor());
    }
}
